package cn.santi.controller;

import cn.santi.domain.JAppointment;
import cn.santi.domain.Staff;
import cn.santi.domain.StaffTime;
import cn.santi.emuns.R;
import cn.santi.excel.ExcelExportUtil;
import cn.santi.service.JAppointmentService;
import cn.santi.service.StaffService;
import cn.santi.service.StaffTimeService;
import cn.santi.utli.DateTool;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/appointment")
public class AppointmentController {
    @Autowired
    private JAppointmentService js;
    @Autowired
    private StaffTimeService st;
    @Autowired
    private StaffService staffService;

    //显示预约详情
    @GetMapping("/showorder")
    public R showorder(int id) {
        QueryWrapper<JAppointment> wrapper = new QueryWrapper<>();
        wrapper.eq("stimeid", id);
        return R.ok(js.list(wrapper));
    }

    @GetMapping("/bystate")
    public R bystate(int id, int state) {
        QueryWrapper<JAppointment> wrapper = new QueryWrapper<>();
        if (state != 4) {
            wrapper.eq("stimeid", id).eq("appointmentstatus", state);
        }
        return R.ok(js.list(wrapper));
    }

    //搜索查询
    @GetMapping("/seekorder")
    public R seekorder(int id, String result) {
        QueryWrapper<JAppointment> wrapper = new QueryWrapper<>();
        wrapper.eq("stimeid", id)
                .and(wrapper1 -> wrapper1.like("contactphone", result).or().like("customername", result));
        return R.ok(js.list(wrapper));
    }

    //修改状态
    @GetMapping("/uptate")
    public R uptate(int id, int staue, int tiemid) {
        if (staue == -1) {
            QueryWrapper<JAppointment> wrapper = new QueryWrapper<>();
            wrapper.eq("id", id);
            js.remove(wrapper);
            QueryWrapper<JAppointment> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("stimeid", tiemid);
            List<JAppointment> list = js.list(wrapper);
            if (list.size() == 0) {
                UpdateWrapper<StaffTime> wrapper2 = new UpdateWrapper<>();
                wrapper2.eq("id", tiemid).set("istrue", 0);
                return R.ok(st.update(wrapper2));
            }
        } else if (staue == -2) {
            QueryWrapper<JAppointment> wrapper = new QueryWrapper<>();
            wrapper.eq("stimeid", tiemid);
            js.remove(wrapper);
            UpdateWrapper<StaffTime> wrapper2 = new UpdateWrapper<>();
            wrapper2.eq("id", tiemid).set("istrue", 0);
            return R.ok(st.update(wrapper2));
        }
        UpdateWrapper<JAppointment> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id).set("appointmentstatus", staue);
        return R.ok(js.update(wrapper));
    }

    @GetMapping("/addAppointment")
    public R addAppointment(JAppointment appointment) {//添加我的预约
//        System.out.println("addAppointment:" + appointment);
        appointment.setSubmittime(DateTool.gettime());
        boolean save = js.save(appointment);
        if (save) {
            LambdaUpdateWrapper<StaffTime> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(StaffTime::getId, appointment.getStimeid())
                    .set(StaffTime::getIstrue, "1");
            st.update(wrapper);

            return R.ok("", "预约成功");
        } else {
            return R.fail("预约失败");
        }
    }

    @GetMapping("/showAppointmentBySnameAndUid")
    public R showAppointmentByUid(String sname, Integer uid) {//根据家政员名称模糊查询我的预约信息
//        System.out.println("showAppointmentBySnameAndUid:" + sname + "," + uid);
        // 根据家政员名称模糊查询家政员ID集合
        List<Integer> sids = staffService.list(Wrappers.<Staff>lambdaQuery().like(Staff::getName, sname))
                .stream()
                .map(Staff::getId)
                .collect(Collectors.toList());

//        System.out.println("sids:" + sids);
        if (CollectionUtils.isEmpty(sids)) {
            return R.fail("没有找到匹配的家政员");
        }
        // 根据用户ID和家政员ID集合查询预约信息，并按提交时间降序排列
        LambdaQueryWrapper<JAppointment> appointmentQueryWrapper = new LambdaQueryWrapper<>();
        appointmentQueryWrapper.eq(JAppointment::getUid, uid)
                .in(JAppointment::getSid, sids)
                .orderByDesc(JAppointment::getSubmittime);

        List<JAppointment> appointments = js.list(appointmentQueryWrapper);
//        System.out.println("appointments:" + appointments);
        return R.ok(appointments);
    }

    @GetMapping("/showAppointmentByTypeAndUid")
    public R showAppointmentByTypeAndUid(String type, Integer uid) {//根据状态排序
        LambdaQueryWrapper<JAppointment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JAppointment::getUid, uid);
        switch (type) {
            case "sort"://全部
                break;
            case "star"://可使用
                wrapper.eq(JAppointment::getAppointmentstatus, 1);
                break;
            case "all"://已核销
                wrapper.eq(JAppointment::getAppointmentstatus, 2);
                break;
            case "gold"://已过期
                wrapper.eq(JAppointment::getAppointmentstatus, 3);
                break;
            case "silver"://系统取消
                wrapper.eq(JAppointment::getAppointmentstatus, 0);
                break;
            default:
                break;
        }
        wrapper.orderByDesc(JAppointment::getSubmittime);
        List<JAppointment> list = js.list(wrapper);
//        System.out.println(list);
        return R.ok(list);
    }

    @GetMapping("/deleteAppointment")
    public R deleteAppointment(Integer id) {//删除我的预约
        JAppointment appointment = js.getById(id);
//        boolean b = js.removeById(id);
//        if (!b) {
//            return R.fail("删除失败");
//        } else {
//            LambdaUpdateWrapper<StaffTime> wrapper = new LambdaUpdateWrapper<>();
//            wrapper.eq(StaffTime::getId, appointment.getStimeid())
//                    .set(StaffTime::getIstrue, "0");
//            st.update(wrapper);
//            return R.ok("", "删除成功");
//        }

        LambdaUpdateWrapper<StaffTime> wrapper = new LambdaUpdateWrapper<>();//用户取消状态改为取消状态
        wrapper.eq(StaffTime::getId, appointment.getStimeid())
                .set(StaffTime::getIstrue, "0");
        boolean update = st.update(wrapper);
        appointment.setAppointmentstatus("5");
        boolean b = js.updateById(appointment);
        if (!b) {
            return R.fail("取消失败");
        } else {
            return R.ok("", "取消成功");
        }
    }

    @GetMapping("/getAppointmentById")
    public R getAppointmentById(Integer id) {//根据id查询预约信息
        return R.ok(js.getById(id));
    }

    @GetMapping("/appointmengetid")
    public void usergetid(HttpServletResponse response,
                          Integer id, String start, String end, String type) throws IOException {//导出用户信息
        LambdaQueryWrapper<JAppointment> wrapper = buildWrapper(id, start, end, type);
        List<JAppointment> one = js.list(wrapper);
        System.out.println(one);
        ExcelExportUtil.sexportExcel(response, one);
    }

    @GetMapping("/getAppointmenCoent")
    public R getAppointmenCoent(Integer id, String start, String end, String type) {//获取我的预约数量
        LambdaQueryWrapper<JAppointment> wrapper = buildWrapper(id, start, end, type);
        List<JAppointment> one = js.list(wrapper);
        System.out.println(one.size());
        return R.ok(one.size());
    }

    private LambdaQueryWrapper<JAppointment> buildWrapper(Integer id, String start, String end, String type) {
        LambdaQueryWrapper<JAppointment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JAppointment::getSid, id);
        wrapper.ge(JAppointment::getSubmittime, start + " 00:00:00");
        wrapper.lt(JAppointment::getSubmittime, end + " 23:59:59");

        if (type.equals("预约成功名单")) {
            wrapper.eq(JAppointment::getAppointmentstatus, 1);
        } else if (type.equals("用户已取消名单")) {
            wrapper.eq(JAppointment::getAppointmentstatus, 5);
        } else if (type.equals("系统取消名单")) {
            wrapper.eq(JAppointment::getAppointmentstatus, 0);
        }else if (type.equals("所有名单")){

        }
//        System.out.println("usergetid:" + id + ":start:" + start + " 00:00:00" + ":end:" + end + " 23:59:59" + "type: " + type);
        return wrapper;
    }
}
